1
'****************************** Module Header ******************************
2 ' Module Name: MainPage.xaml.vb
3 ' Project: VBSL4DataFormCancelButton
4 ' Copyright (c) Microsoft Corporation.
6 ' MainPage's code hehind file.
8 ' This source is subject to the Microsoft Public License.
9 ' See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
10 ' All other rights reserved.
12 ' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
13 ' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
14 ' WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
15 '***************************************************************************
17 Imports System
.Collections
.ObjectModel
18 Imports System
.ComponentModel
19 Imports System
.ComponentModel
.DataAnnotations
21 Partial
Public Class MainPage
26 AddHandler Loaded
, AddressOf MainPage_Loaded
30 Private Sub MainPage_Loaded(ByVal sender
As Object, ByVal e
As RoutedEventArgs
)
31 myDataForm
.ItemsSource
= New Employee()
36 ' Your class inherit from IEditableObject to achieve the purpose
37 Public Class EmployeeItem
38 Implements INotifyPropertyChanged
39 Implements IEditableObject
40 Private Structure ItemData
41 Friend EmployeName
As String
42 Friend Gender
As String
44 Friend OnboardDate
As DateTime
47 Private originalItem
As ItemData
48 Private currentItem
As ItemData
51 Me.New("Employee Name", 25, "Male", DateTime
.Now
)
54 Public Sub New(ByVal empName
As String, ByVal empAge
As Integer, ByVal empGender
As String, ByVal empOnboardDate
As DateTime
)
55 EmployeeName
= empName
58 OnboardDate
= empOnboardDate
61 Public Property EmployeeName() As String
63 Return currentItem
.EmployeName
65 Set(ByVal value
As String)
66 If currentItem
.EmployeName
<> value
Then
67 currentItem
.EmployeName
= value
68 NotifyPropertyChanged("EmployeName")
73 Public Property Gender() As String
75 Return currentItem
.Gender
77 Set(ByVal value
As String)
78 If currentItem
.Gender
<> value
Then
79 currentItem
.Gender
= value
80 NotifyPropertyChanged("Gender")
86 <Range(10, 80, ErrorMessage
:="Employee's Age range should within 10 to 80")> _
87 <Display(Name
:="Age", Description
:="Employee's Age")> _
88 Public Property Age() As Integer
90 Return currentItem
.Age
92 Set(ByVal value
As Integer)
93 If currentItem
.Age
<> value
Then
94 Validator
.ValidateProperty(value
, New ValidationContext(Me, Nothing, Nothing) With { _
97 currentItem
.Age
= value
98 NotifyPropertyChanged("Age")
103 Public Property OnboardDate() As DateTime
105 Return currentItem
.OnboardDate
107 Set(ByVal value
As DateTime
)
108 If value
<> currentItem
.OnboardDate
Then
109 currentItem
.OnboardDate
= value
110 NotifyPropertyChanged("OnboardDate")
116 #Region
"INotifyPropertyChanged Members"
118 Public Event PropertyChanged
As PropertyChangedEventHandler _
119 Implements INotifyPropertyChanged
.PropertyChanged
121 Private Sub NotifyPropertyChanged(ByVal info
As String)
122 RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info
))
127 #Region
"IEditableObject Members"
129 ' Copy the original value before editing
130 Public Sub BeginEdit() Implements IEditableObject
.BeginEdit
132 originalItem
= currentItem
135 ' Restore the original value if edit operation is cancelled.
136 Public Sub CancelEdit() Implements IEditableObject
.CancelEdit
137 currentItem
= originalItem
138 NotifyPropertyChanged("")
142 Public Sub EndEdit() Implements IEditableObject
.EndEdit
150 Public Class Employee
151 Inherits
ObservableCollection(Of EmployeeItem
)
153 Add((New EmployeeItem("Steven", 20, "Male", New DateTime(2010, 9, 1))))
154 Add((New EmployeeItem("Vivian", 26, "Female", New DateTime(2008, 5, 1))))
155 Add((New EmployeeItem("Bill", 28, "Male", New DateTime(2006, 2, 2))))
156 Add((New EmployeeItem("Janney", 30, "Female", New DateTime(2004, 3, 10))))
157 Add((New EmployeeItem("Bob", 40, "Male", New DateTime(2009, 5, 27))))
158 Add((New EmployeeItem("Jonathan", 27, "Male", New DateTime(2000, 10, 25))))